package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HOrder;
import com.zhongwang.mms.module.basic.service.BoardService;
import com.zhongwang.mms.module.half.model.HInBillModel;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.model.PrintListModel;
import com.zhongwang.mms.module.purchase.model.VWProject;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.StringUtils;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static com.zhongwang.mms.config.timingtask.StatisticalTimingTask.getVWProjectByCode;

/**
 * @description: 铝料入库
 * @author: th
 * @time: 2020/6/12 10:18
 */
@Service
public class InAluminWarService {

    @Autowired
    private BoardService boardService;
    @Autowired
    private HOrderService hOrderService;
    @Autowired
    private HOrderDetailService hOrderDetailService;
    @Autowired
    private HInventoryService hInventoryService;
    @Autowired
    private LogService logService;


    @Transactional
    public JsonResult saveHInBillModel(HInBillModel hInBillModel) {
        JsonResult jsonResult = new JsonResult();
        List<String> result = new ArrayList<>();
        PrintListModel printListModel = new PrintListModel();
        //验证物料描述、数量、单位是否为空
        for (var i = 0; i < hInBillModel.getInsertData().size(); i++) {
            if (hInBillModel.getInsertData().get(i).getOdaMatName() == null || hInBillModel.getInsertData().get(i).getOdaMatName() == "") {
                result.add("\n第" + (i + 1) + "行" + " 类型 为空");
            }
            if (hInBillModel.getInsertData().get(i).getOdaAvaNum() == null) {
                result.add("\n第" + (i + 1) + "行" + " 数量 为空");
            } else if (hInBillModel.getInsertData().get(i).getOdaAvaNum() <= 0) {
                result.add("\n第" + (i + 1) + "行" + " 数量 必须大于0");
            }
            if (hInBillModel.getInsertData().get(i).getOdaStockTypeName() == null || hInBillModel.getInsertData().get(i).getOdaStockTypeName() == "") {
                result.add("\n第" + (i + 1) + "行" + " 在库类型 为空");
            } else if (hInBillModel.getInsertData().get(i).getOdaStockTypeName().equals("项目在库") && (hInBillModel.getInsertData().get(i).getOdaProCode() == null || hInBillModel.getInsertData().get(i).getOdaProCode() == "")) {
                result.add("\n第" + (i + 1) + "行" + " 项目编号 为空");
            }
            if (hInBillModel.getInsertData().get(i).getOdaWeight() == null) {
                result.add("\n第" + (i + 1) + "行" + " 总重量 为空");
            } else if (hInBillModel.getInsertData().get(i).getOdaWeight() <= 0) {
                result.add("\n第" + (i + 1) + "行" + " 总重量 必须大于0");
            }else if (hInBillModel.getInsertData().get(i).getOdaStockTypeName() != null && hInBillModel.getInsertData().get(i).getOdaStockTypeName() != "" && hInBillModel.getInsertData().get(i).getOdaStockTypeName().equals("项目在库") && hInBillModel.getInsertData().get(i).getOdaPrice() == null) {
                result.add("\n第" + (i + 1) + "行" + " 单价 为空");
            } else if (hInBillModel.getInsertData().get(i).getOdaStockTypeName() != null && hInBillModel.getInsertData().get(i).getOdaStockTypeName() != "" && hInBillModel.getInsertData().get(i).getOdaStockTypeName().equals("项目在库") && hInBillModel.getInsertData().get(i).getOdaPrice() <= 0) {
                result.add("\n第" + (i + 1) + "行" + " 单价 必须大于0");
            } else if (hInBillModel.getInsertData().get(i).getOdaStockTypeName() != null && hInBillModel.getInsertData().get(i).getOdaStockTypeName() != "" && hInBillModel.getInsertData().get(i).getOdaStockTypeName().equals("项目在库") && hInBillModel.getInsertData().get(i).getOdaPrice() != null && hInBillModel.getInsertData().get(i).getOdaAmount() == null) {
                result.add("\n第" + (i + 1) + "行" + " 金额 为空");
            } else if (hInBillModel.getInsertData().get(i).getOdaStockTypeName() != null && hInBillModel.getInsertData().get(i).getOdaStockTypeName() != "" && hInBillModel.getInsertData().get(i).getOdaStockTypeName().equals("项目在库")){
                DecimalFormat df = new DecimalFormat("#.00");
                BigDecimal num = new BigDecimal(hInBillModel.getInsertData().get(i).getOdaWeight());
                num = num.setScale(3,BigDecimal.ROUND_HALF_UP);

                DecimalFormat decimalFormat = new DecimalFormat("#"); // 创建格式化对象并设置模式为"#"
                String formattedNumber = decimalFormat.format(hInBillModel.getInsertData().get(i).getOdaPrice()); // 将double转换成字符串形式
                int decimalLength = formattedNumber.length() - formattedNumber.indexOf('.') - 1; // 计算小数点后的位数
                BigDecimal price = new BigDecimal(hInBillModel.getInsertData().get(i).getOdaPrice());
                price = price.setScale(decimalLength,BigDecimal.ROUND_HALF_UP);
                BigDecimal resultTotalPrice = num.multiply(price);
                resultTotalPrice = resultTotalPrice.setScale(2,BigDecimal.ROUND_HALF_UP);
                //BigDecimal totalPrice = new BigDecimal(df.format(hInBillModel.getInsertData().get(i).getOdaAmount()));
                BigDecimal totalPrice = new BigDecimal(hInBillModel.getInsertData().get(i).getOdaAmount());
                totalPrice = totalPrice.setScale(2,BigDecimal.ROUND_HALF_UP);

                if (totalPrice.compareTo(resultTotalPrice) != 0)
                    result.add("\n第" + (i + 1) + "行" + " 金额不正确");
            }

            if((hInBillModel.getInsertData().get(i).getOdaProCode() != null && hInBillModel.getInsertData().get(i).getOdaProCode() != "")&& (hInBillModel.getInsertData().get(i).getOdaProName() == null || hInBillModel.getInsertData().get(i).getOdaProName() == "")){
                result.add("\n第" + (i + 1) + "行" + " 项目名称 为空");
            }
            if (hInBillModel.getInsertData().get(i).getOdaProCode() != null &&
                    hInBillModel.getInsertData().get(i).getOdaProCode() != "") {
                if (boardService.isNotExist(boardService.projectCodeHandle(hInBillModel.getInsertData().get(i).getOdaProCode()))) {
                    result.add("\n第" + (i + 1) + "行" + " 项目编码不存在 请修改");
                }else {
                    VWProject vmProject = getVWProjectByCode(boardService.projectCodeHandle(hInBillModel.getInsertData().get(i).getOdaProCode()));
                    if (!vmProject.getName().equals(hInBillModel.getInsertData().get(i).getOdaProName())) {
                        result.add("\n第" + (i + 1) + "行" + " 项目项目名称不正确 请修改");
                    }
                }
            }
            if (hInBillModel.getInsertData().get(i).getOdaProCode() != null && StringUtils.isContainChinese(hInBillModel.getInsertData().get(i).getOdaProCode())) {
                result.add("\n第" + (i + 1) + "行" + " 项目编码不可出现汉字 违规字符");
            }
        }
        if (result.size() > 0) {
            result.add("\n单据保存失败，请先维护以上信息！");
            printListModel.setList(result);
            printListModel.setId(hInBillModel.getFormData().getOdrId());
            jsonResult.setData(printListModel);
            return jsonResult;
        }
        hInBillModel.getFormData().setOdrType(1);//1入库2出库
        hInBillModel.getFormData().setOdrStatus(0);//1调拨0其他
        //hInBillModel.getFormData().setOdrOutType(1);//采购入库
        //保存主单信息
        HOrder hOrderForm = hOrderService.saveHOrder(hInBillModel);

        HOrderDetailModel hOrderDetailModel;
        //遍历表单内容
        for (var i = 0; i < hInBillModel.getInsertData().size(); i++) {

            //获取表单明细
            hOrderDetailModel = hInBillModel.getInsertData().get(i);
            //获取主单ID
            hOrderDetailModel.setOdrId(hInBillModel.getFormData().getOdrId());
            //获取仓库
            hOrderDetailModel.setOdaWhiId(hInBillModel.getFormData().getOdaWhiId());
            //物料分类
            hOrderDetailModel.setOdaMatType(4);//1是外购件、2是外协件、3是制作件
            //物料编码
            hOrderDetailModel.setOdaMatCode("1111111111");
            //红冲标识
            hOrderDetailModel.setOdaRedFlag(0);
            //保存表单明细
            hOrderDetailService.saveHOrderDetail(hOrderDetailModel);
            //保存库存信息
            hInventoryService.saveHInventory(hOrderDetailModel);

        }
        result.add("单据保存成功！");
        logService.saveLog("【成品库】铝料入库", "入库单据编码：" + hOrderForm.getOdrCode());
        printListModel.setList(result);
        printListModel.setId(hInBillModel.getFormData().getOdrId());
        jsonResult.setData(printListModel);
        return jsonResult;
    }
}
